package com.cloudstore.eccom.avic;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.concurrent.TimeUnit;

/**
 * @author dusongsong
 * @date 2023年12月07日 10:23
 */
public class DoingWfUtil{

    private static BaseBean baseBean = new BaseBean();
    private static final String URL = "http://uat.fsyoa.fsygroup.com/rest/ofs/ReceiveRequestInfoByJson";

    public static JSONObject sendDoingWorkflow(JSONObject paramsJson) {
        paramsJson.put("syscode","OA");
        paramsJson.put("isremark","0");
        paramsJson.put("viewtype","0");
        paramsJson.put("receivets",System.currentTimeMillis());
        baseBean.writeLog(">>>>>handleOKHttpRequest>>>>paramsJson="+paramsJson);
        return sendOKHttpRequest(paramsJson);
    }

    //调用接口
    public static JSONObject sendOKHttpRequest(JSONObject params) {
        JSONObject resultJson = new JSONObject();
        String code = "failure";
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

    public static JSONObject getNodeBase(String requestid) {
        JSONObject dataJson = new JSONObject();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select n.id,n.nodename,b.currentnodetype,b.requestname from workflow_requestbase b left join workflow_nodebase n on b.currentnodeid=n.id where b.requestid=?", requestid);
        rs.next();
        dataJson.put("nodeid", Util.null2String(rs.getString("id")));
        dataJson.put("currentnodetype", Util.null2String(rs.getString("currentnodetype")));
        dataJson.put("nodename", Util.null2String(rs.getString("nodename")));
        dataJson.put("requestname", Util.null2String(rs.getString("requestname")));
        return dataJson;
    }

}
